package com.st.warehouse.utils;

import com.alibaba.fastjson.JSONArray;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
    /**
     * 对象为空判断
     * @param obj
     * @return boolean
     */
    public static boolean isEmpty(Object obj) {
        return obj == null || obj.toString().trim().equals("");
    }

    /**
     * 对象不为空判断
     * @param obj
     * @return boolean
     */
    public static boolean isNotEmpty(Object obj) {
        return obj != null && !"".equals(obj);
    }

    /**
     * JSONArray不为空判断
     * @param list
     * @return boolean
     */
    public static boolean isNotEmptyJSONArray(JSONArray list) {
        return list != null && list.size() > 0;
    }

    /**
     * 是否为JSON判断
     * @param str
     * @return boolean
     */
    public static boolean isJson(String str) {
        boolean result = false;
        if (StringUtils.isNotEmpty(str)) {
            str = str.trim();
            if (str.startsWith("{") && str.endsWith("}")) {
                result = true;
            } else if (str.startsWith("[") && str.endsWith("]")) {
                result = true;
            }
        }
        return result;
    }

    /**
     * 字符串转URL编码
     * @param str
     * @return URL编码后的字符
     */
    public static String stringToURLCode(String str) {
        if (StringUtils.isEmpty(str)) return "";
        String newString = null;
        // 使用 URLEncoder 进行 URL 编码
        try {
            String encodedQueryText = URLEncoder.encode(str, StandardCharsets.UTF_8.toString());
            newString = encodedQueryText;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return newString;
    }
    public static int getChineseCharacterCount(String input) {
        // 使用正则表达式匹配中文字符
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
        Matcher matcher = pattern.matcher(input);
        int chineseCharacterCount = 0;
        while (matcher.find()) {
            chineseCharacterCount++;
        }

        return chineseCharacterCount;
    }
    /**
     * byte数组转字符
     * @param bytes [AE,01,FF]
     * @return "AE01FF"
     */
    public static String getString(String[] bytes, int start, int end) {
        String result = "";
        for (int i = start; i <= end; i++) result = result + bytes[i];
        return result.toString();
    }
    /**
     * 16进制转换成为string类型字符串（汉字）
     * @param str
     * @return
     */
    public static String hexStringToString(String str) {
        if (str == null || str.equals("")) {
            return null;
        }
        str = str.replace(" ", "");
        byte[] baKeyword = new byte[str.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            str = new String(baKeyword, "UTF-8");
            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return str;
    }
}
